// Copyright 2023 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef _SOCKET_BASE_H_
#define _SOCKET_BASE_H_

#include <string>
#include <vector>
#include <iostream>
#include <memory>
#include "signal.h"
#include "Log.h"
#include "ClientCommon.h"

namespace master {

#define ENC_OK 0
#define ENC_FAIL -1
#define NUM_SCALE_BIG_1000000(a) (a * 1000 * 1000)
#define SOCKET_BUF_BLOCK 64
#define MAX_LISTEN_NUM 1024
#define EAGAIN_TIME_OUT 2000
#define ERROR_FD (-1)
#define ETH0 "eth0"

static const int IS_RUNNING = 1;
static const int IS_OVER = 0;
static const uint32_t TIME_OUT_INTERVAL = 50;
static const uint32_t MAX_PORT_NUM = 65535;
static const uint32_t SRC_BUF_LEN = 1024 * 1024;
static const uint32_t DST_BUF_LEN = 1024 * 1024;

class BaseSocket {
public:
    BaseSocket();

    virtual ~BaseSocket();

    const int socketGetFd() { return fd; }

    void socketSetParamInfo(uint16_t ipPort);

    int socketSetFd();

    int setReUseAddr(void);

    int setBindDevice(void);

    int socketSetSockOpt();

    int setTimeOut(void);

    int socketBind();

    int socketListen();

    int setFdUnblock(int connFd);

    int socketAccept(char *ipAddr, uint16_t *ipPort);

    int socketRecv(int clientFd, uint8_t *recvBuf, uint32_t recvBufLen, int *readLen);

    int socketSend(int clientFd, uint8_t *sendBuf, uint32_t sendLen, uint32_t *sendRetLen);

    void socketClose();

    void socketClose(int sockFd);

    bool isClose(const int iFd);

    uint32_t eagainCnt;
private:
    int fd;
    uint16_t port;
};
};
#endif